// const BASE_URL = 'http://localhost:8080'
const BASE_URL = 'https://api.taishow.cc/talent'
export const handleProxy = (url) => {
  return BASE_URL + url
}
export const handleImage = (content) => {
  // 兼容 content 为空或非字符串的情况，避免调用 replace 报错
  if (content == null) return ''
  const str = typeof content === 'string' ? content : String(content)
  const regex = /<img [^>]*src=['"]([^'"]+)['"][^>]*>/gi
  try {
    return str.replace(regex, (match, src) => {
      // 若未匹配到 src 或为绝对/协议相对地址或 data URI，则原样返回
      if (!src || /^(https?:|data:|\/\/)/i.test(src)) {
        return match
      }
      const newSrc = handleProxy(src)
      return match.replace(src, newSrc)
    })
  } catch (e) {
    // 出现异常时返回原始字符串，保证不影响页面渲染
    return str
  }
}
/**
 * 获取首页设置
 */
export const getSettings = () => {
  return uni.request({
    url: `${BASE_URL}/api/common/settings`,
  })
}

/**
 * 获取社区列表
 */
export const getComunityList = () => {
  return uni.request({
    url: `${BASE_URL}/api/common/communities?page=1&pageSize=200`,
  })
}

/**
 * 获取筛选列表
 */
export const getFilterList = () => {
  return uni.request({
    url: `${BASE_URL}/api/common/filterList`,
  })
}

/**
 * 获取点位列表
 */
export const getSpotList = () => {
  return uni.request({
    url: `${BASE_URL}/api/common/spots`,
    method: 'GET',
  })
}
/**
 * 获取点位详情
 */
export const getSpotDetail = (id) => {
  return uni.request({
    url: `${BASE_URL}/api/common/spots/${id}`,
    method: 'GET',
  })
}

// -------------------服务接口-------------------
/**
 * 获取服务列表
 */
export const getServiceList = (name) => {
  return uni.request({
    url: `${BASE_URL}/api/common/serviceTree?${name ? `name=${name}` : ''}`,
  })
}
/**
 * 获取服务详情
 */
export const getServiceDetail = (id) => {
  return uni.request({
    url: `${BASE_URL}/api/common/services/${id}`,
  })
}
/**
 * 获取站点列表
 */
export const getCategoryList = () => {
  return uni.request({
    url: `${BASE_URL}/api/common/categories?type=service`,
  })
}

/**
 * 获取顶级菜单
 */
export const getTopMenu = () => {
  return uni.request({
    url: `${BASE_URL}/api/common/top-menu`,
    method: 'GET',
  })
}

/**
 * 分页查询公告
 */
export const getNoticeList = (page, pageSize, title) => {
  let url = `${BASE_URL}/api/common/notices?page=${page}&pageSize=${pageSize}`
  if (title) {
    url += `&title=${encodeURIComponent(title)}`
  }
  return uni.request({
    url: url,
    method: 'GET',
  })
}

/**
 * 查询公告详情
 */
export const getNoticeDetail = (id) => {
  return uni.request({
    url: `${BASE_URL}/api/common/notices/${id}`,
    method: 'GET',
  })
}

/**
 * 查询轮播公告
 */
export const getNoticeBanner = () => {
  return uni.request({
    url: `${BASE_URL}/api/common/notice-banners`,
    method: 'GET',
  })
}
